from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.alert import Alert
from selenium.common.exceptions import (
    UnexpectedAlertPresentException,
    NoAlertPresentException,
)


class SwitcheNet:
    def __init__(self):
        self.driver = self.open_login_page()
        pass

    def open_login_page(self):
        # 指定Firefox的路径，例如在Windows上：
        firefox_path = "/opt/apps/org.mozilla.firefox-nal/files/firefox-nal/firefox"

        # 使用Service来设置路径
        # service = Service(executable_path="./geckodriver", firefox_binary=firefox_path)

        opt = Options()
        opt.binary_location = firefox_path

        sv = Service("/home/angela/app/geckodriver", log_output="gecko.log")
        # 设置浏览器驱动（需要下载对应浏览器的驱动）
        driver = webdriver.Firefox(service=sv, options=opt)  # 或 Firefox(), Edge() 等

        # 打开登录页面
        driver.get(
            "http://59.216.1.130:8080/portal/templatePage/20220526120702008/login_custom.jsp"
        )

        WebDriverWait(driver, 2).until(
            EC.visibility_of_element_located((By.ID, "id_lable_logoutbutton_auth"))
        )

        driver.minimize_window()

        return driver

    def select_value(self, keyword):
        self.driver.get(
            "http://59.216.1.130:8080/portal/templatePage/20220526120702008/login_custom.jsp"
        )

        all_windows = self.driver.window_handles
        print("Window handles", all_windows)
        if len(all_windows) > 1:
            self.driver.switch_to.window(all_windows[1])
            print("关闭：", all_windows[1])
            self.driver.close()
            self.driver.switch_to.window(all_windows[0])

        try:
            # 触发可能弹出对话框的操作
            # 打开页面后先下线，可能之前是已登录的状态
            signout_btn = self.driver.find_element(By.ID, "id_lable_logoutbutton_auth")
            signout_btn.click()

            time.sleep(1)

            print("点击下线")

            # 等待并处理对话框
            alert = Alert(self.driver)

            # alert.accept()  # 或者
            alert.dismiss()  # 根据需要选择接受或拒绝对话框
        except UnexpectedAlertPresentException:
            print("有弹窗")
        except NoAlertPresentException:
            print("无弹窗")

        # 找到用户名和密码输入框并输入信息
        username = self.driver.find_element(
            By.ID, "id_userName"
        )  # 或 By.ID, By.XPATH等
        password = self.driver.find_element(By.ID, "id_userPwd")

        username.send_keys("fgwzp1")
        password.send_keys("Sep91700")

        # 定位下拉菜单元素
        dropdown = Select(
            self.driver.find_element(By.ID, "id_serviceType")
        )  # 也可以用其他定位方式

        # 选择选项的方法：
        # 1. 通过可见文本选择
        dropdown.select_by_visible_text(keyword)

        # # 2. 通过 value 属性选择
        # dropdown.select_by_value("选项的value值")

        # # 3. 通过索引选择（从0开始）
        # dropdown.select_by_index(1)  # 选择第二个选项

        # 提交表单（找到登录按钮并点击）
        login_button = self.driver.find_element(By.ID, "id_lable_loginbutton_auth")
        login_button.click()

        # 等待登录完成
        time.sleep(2)

    def change(self, value):
        self.select_value(value)

    def quit(self):
        self.driver.quit()
